var ctx=window.daijia.ctx;
var companyId=window.daijia.companyId;
var hasdaijia=window.hasdaijia;
var haszhuanche=window.haszhuanche;
var carTypeIdAreaIds ="";
$(document).ready(function(){
	queryCarTypes();
	$(document).on("change",".carType_select",function() {
		var carType_divs=$(".tab-pane");
		var flag=false;
		carTypeIdAreaIds ="";
		for(var i=0;i<carType_divs.length;i++){
			var c=carType_divs[i];
			var carTypeId =$(c).attr("carTypeId");
			var area_divs = $(c).children("div");
			var carTypeArea="";
			$(area_divs).each(function(index,element){
				var areas = area_divs[index];
				var d = $(areas).children("div").children("select");
				var areaId = $(d).find("option:selected").val();
				if (areaId != null && areaId != "") {
					flag=true;
					var type = $(areas).attr("type");
					carTypeArea += ",'"+type+"':"+areaId;
				}
			});
			if (carTypeArea != "") {
				carTypeIdAreaIds +="{'carTypeId':"+carTypeId+carTypeArea;
			
				carTypeIdAreaIds += "},";
			}
			
		}
		if (flag) {
			carTypeIdAreaIds = "["+carTypeIdAreaIds
		
			carTypeIdAreaIds = carTypeIdAreaIds.substring(0,carTypeIdAreaIds.length-1);
		
			carTypeIdAreaIds += "]";
		}
		console.debug("carTypeIdAreaIds:"+carTypeIdAreaIds);
	});
});
$(document).on("change","#companyId",function(){
	companyId=$("#companyId").val();
	queryCarTypes();
});
$(document).on("click","li",function(){
	var a=$(this).children("a");
	var href=$(a).attr("href");
	
	var tab_pane=$(".tab-pane");
	for(var i=0;i<tab_pane.length;i++){
		var t=tab_pane[i];
		var id=$(t).attr("id");
		if(href!="#"+id){
			$(t).css("display","none");
		}else{
			$(t).css("display","block");
		}
	}
});
function queryCarTypes(){
	
	$.ajax({
        url : ctx+'/zhuanche/queryCarTypes',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"companyId":companyId,
        },
        success : function(result){
        	var carTypeList="";
        	$("#carTypeList").empty();
        	$("#carType_div").empty();
        	if(result.success){
        		$(result.carTypes).each(function(index,carType){
        			var carType_div="";
        			var carTypeName=carType.typeName;
        			carTypeList+="<li><a href='#zhuanTab"+index+"' data-toggle='tab'>"+carTypeName+"</a></li>";
        			if(index==0){
        				carType_div="<div id='zhuanTab"+index+"' class='tab-pane active' carTypeId='"+carType.id+"'>";
        			}else{
        				carType_div="<div id='zhuanTab"+index+"' class='tab-pane' carTypeId='"+carType.id+"' style='display:none'>";
        			}
        			carType_div+="<div class='form-group' carTypeId='"+carType.id+"' type='tzAreaId'>"+
		                    "<label class='col-sm-3 control-label'>普通收费:</label>"+    
		                   	"<div class='col-sm-4'>"+
			                    "<select class='form-control carType_select'>"+
			                    	"<option value=''>--请选择--</option>";
			                    	for(var i=0;i<result.arealist.length;i++){
			                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
			                    	}
        	  carType_div+="</select>"+
		                	"</div>"+
		               "</div>"+
	                   "<div class='form-group' carTypeId='"+carType.id+"' type='rzAreaId'>"+
		                    "<label class='col-sm-3 control-label'>日租收费:</label>"+
		                    "<div class='col-sm-4'>"+
			                "<select class='form-control carType_select'>"+
			                	"<option value=''>--请选择--</option>";
			                	for(var i=0;i<result.arealist.length;i++){
		                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
		                    	}
        	  carType_div+="</select>"+
		                        "</div>"+
		                        "</div>"+
		                        "<div class='form-group' carTypeId='"+carType.id+"' type='brzAreaId'>"+
		                        "<label class='col-sm-3 control-label'>半日租收费:</label>"+ 
		                        "<div class='col-sm-4'>"+
		                        "<select class='form-control carType_select'>"+
			               "<option value=''>--请选择--</option>";
			               for(var i=0;i<result.arealist.length;i++){
	                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
	                    	}
           	  carType_div+="</select>"+
		                	"</div>"+
		               "</div>"+
		                "<div class='form-group' carTypeId='"+carType.id+"' type='jjAreaId'>"+
		                 "<label class='col-sm-3 control-label'>接机收费:</label>   "+ 
		                  "<div class='col-sm-4'>"+
			               "<select class='form-control carType_select'>"+
			                "<option value=''>--请选择--</option>";
			                for(var i=0;i<result.arealist.length;i++){
	                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
	                    	}
        	  carType_div+="</select>"+
		                	"</div>"+
		                "</div>"+
		               "<div class='form-group' carTypeId='"+carType.id+"' type='sjAreaId'>"+
		                   "<label class='col-sm-3 control-label'>送机收费:</label>    "+
		                   	"<div class='col-sm-4'>"+
			                "<select class='form-control carType_select'>"+
			               "<option value=''>--请选择--</option>";
			               for(var i=0;i<result.arealist.length;i++){
	                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
	                    	}
           	   carType_div+="</select>"+
		                "</div>"+
		               "</div>"+
		               "<div class='form-group' carTypeId='"+carType.id+"' type='jzAreaId'>"+
		                  "<label class='col-sm-3 control-label'>接站收费:</label> "+   
		                   "<div class='col-sm-4'>"+
			                "<select class='form-control carType_select'>"+
			                "<option value=''>--请选择--</option>";
			                for(var i=0;i<result.arealist.length;i++){
	                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
	                    	}
        	   carType_div+="</select>"+
		               "</div>"+
		               "</div>"+
		               "<div class='form-group' carTypeId='"+carType.id+"' type='szAreaId'>"+
		                  "<label class='col-sm-3 control-label'>送站收费:</label>    "+
		                  "<div class='col-sm-4'>"+
			                  "<select class='form-control carType_select'>"+
			                    	"<option value=''>--请选择--</option>";
			                    	for(var i=0;i<result.arealist.length;i++){
			                    		carType_div+="<option value='"+result.arealist[i].id+"'>"+result.arealist[i].name+"</option>";
			                    	}
               carType_div+="</select>"+
		                	"</div>"+
		                "</div>"+
	                "</div>";
        			
	                $("#carType_div").append(carType_div);
        		});
        		
        		$("#carTypeList").append(carTypeList);
        		
        		
        	}else{
        		$("#carTypeList").append("<span style='color:red'>"+result.message+"</span>");
        	}
        }
        
	});
}

$("#submit_button").click(function(){
	if(parseInt($("#parallelOrderNumber").val()) < 1){
        $.scojs_message("并行订单量不能小于1!", $.scojs_message.TYPE_ERROR);
        return false;
    }

    if(parseInt($("#limitAmounts").val())>parseInt($("#parallelOrderNumber").val())){
        $.scojs_message("一次性下单量不能超过并行下单量!", $.scojs_message.TYPE_ERROR);
        return false;
    }
	$("#submit_button").attr("disabled",true);
	if (carTypeIdAreaIds != "") {
		$.ajax({
			type: "POST",
			url: ctx+"/member/createGrade",
			data: {
				name:$("#name").val(),
				parallelOrderNumber:$("#parallelOrderNumber").val(),
				companyId:$("#companyId").val(),
				companyName:$('#companyId').find("option:selected").text(),
				defaultGrade:$("input[type='radio']:checked").val(),
				limitAmounts:$("#limitAmounts").val(),
				daijiaAreaId:$("#daijiaAreaId").val(),
				carTypes:carTypeIdAreaIds
			},	
			dataType: "json",
			success: function(data){ 
				if(data.success){
					$.scojs_message('保存成功', $.scojs_message.TYPE_OK);
					setTimeout(function(){
						window.location=ctx+'/member/gradelist';
					},1000);
				}else{
					$("#submit_button").removeAttr("disabled");
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				}
			}
		});
	} else {
		$.ajax({
	        type: "POST",
	        url: ctx+"/member/createGrade",
	        data: {
	        	name:$("#name").val(),
	        	parallelOrderNumber:$("#parallelOrderNumber").val(),
	        	companyId:$("#companyId").val(),
	       	 	companyName:$('#companyId').find("option:selected").text(),
	       	 	defaultGrade:$("input[type='radio']:checked").val(),
	       	 	limitAmounts:$("#limitAmounts").val(),
	       	 	daijiaAreaId:$("#daijiaAreaId").val()
	        },	
	        dataType: "json",
	        success: function(data){ 
	        	 if(data.success){
	                 $.scojs_message('保存成功', $.scojs_message.TYPE_OK);
	                 setTimeout(function(){
	                	 window.location=ctx+'/member/gradelist';
	                 },1000);
	             }else{
	             	$("#submit_button").removeAttr("disabled");
	             	$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	             }
	        }
	    });
	}
});
